package org.eclipse.epsilon.emc.spreadsheets.csv.read;

import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.eclipse.epsilon.common.util.FileUtil;
import org.eclipse.epsilon.emc.spreadsheets.SpreadsheetRow;
import org.eclipse.epsilon.emc.spreadsheets.csv.CsvModel;
import org.eclipse.epsilon.emc.spreadsheets.csv.CsvWorksheet;
import org.eclipse.epsilon.eol.exceptions.models.EolModelElementTypeNotFoundException;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;

public class ReadDifferingLengthRowTest
{
	private static CsvModel model = null;
	private static String filePath = "resources/CsvFile.txt";

	@Before
	public void setUpFileAndLoad() throws Exception
	{
		String header = "c0,c1,c2,c3,c4,c5" + System.getProperty("line.separator");
		String firstLine = "v0,v1,v2" + System.getProperty("line.separator");
		String secondLine = "v0" + System.getProperty("line.separator");
		String thirdLine = ",,v2" + System.getProperty("line.separator");
		String contents = header + firstLine + secondLine + thirdLine;
		FileUtil.setFileContents(contents, new File(filePath));
		this.setUp();
	}

	private void setUp() throws Exception
	{
		model = new CsvModel();
		model.setFile(filePath);
		model.setConfigurationFile("resources/CsvFileTestConfig.xml");
		model.setHasHeader(true);
		model.setStoredOnDisposal(false);
		model.setName("MODEL");
		model.load();
	}

	@Test
	public void readAllRows() throws EolModelElementTypeNotFoundException
	{
		List<SpreadsheetRow> rows = (List<SpreadsheetRow>) model.getAllOfKind(CsvWorksheet.NAME);
		assertTrue(CollectionUtils.isNotEmpty(rows));
		assertTrue(rows.size() == 3);
		this.assertFirstRow(rows.get(0));
		this.assertSecondRow(rows.get(1));
		this.assertThirdRow(rows.get(2));
	}

	private void assertFirstRow(SpreadsheetRow row)
	{
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c0")).equals("v0"));
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c1")).equals("v1"));
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c2")).equals("v2"));
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c3")) == null);
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c4")) == null);
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c5")) == null);
	}

	private void assertSecondRow(SpreadsheetRow row)
	{
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c0")).equals("v0"));
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c1")) == null);
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c2")) == null);
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c3")) == null);
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c4")) == null);
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c5")) == null);
	}

	private void assertThirdRow(SpreadsheetRow row)
	{
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c0")).equals(""));
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c1")).equals(""));
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c2")).equals("v2"));
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c3")) == null);
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c4")) == null);
		assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c5")) == null);
	}

	@AfterClass
	public static void tearDown() throws Exception
	{
		FileUtil.setFileContents("", new File(filePath));
	}

}
